/*
Xern Entity Relocation Network
Copyright (C) 2011 Ronie Salgado <roniesalg@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef XTOOL_GPU_HPP
#define XTOOL_GPU_HPP

#include <string>
#include <boost/shared_ptr.hpp>
#include <GL/glew.h>
#include <GL/gl.h>

namespace XernRay
{
    class DataBuffer
    {
    public:
        virtual ~DataBuffer() {}

        virtual size_t GetSize() const = 0;
        virtual void Write(const void *src, size_t offset, size_t amount) = 0;
        virtual void Read(void *dst, size_t offset, size_t amount) = 0;
        virtual void Activate(int unit) = 0;
    };

    class TextureBuffer: public DataBuffer
    {
    public:
        TextureBuffer(int width, int height, int bpp, GLenum internalFormat, GLenum format, GLenum type);
        ~TextureBuffer();

        virtual size_t GetSize() const;
        virtual void Write(const void *src, size_t offset, size_t amount);
        virtual void Read(void *dst, size_t offset, size_t amount);
        virtual void Activate(int unit);

        int GetWidth() const
        {
            return width;
        }

        int GetHeight() const
        {
            return height;
        }

    private:
        friend class TargetBuffer;

        int width, height;
        int bpp;
        GLuint textureObject;
        GLenum internalFormat, format, type;
    };

    class LinearBuffer: public DataBuffer
    {
    public:
        LinearBuffer(size_t size, GLenum format);
        ~LinearBuffer();

        virtual size_t GetSize() const;
        virtual void Write(const void *src, size_t offset, size_t amount);
        virtual void Read(void *dst, size_t offset, size_t amount);
        virtual void Activate(int unit);

    private:
        GLuint bufferObject, textureObject;
        size_t size;
    };

    class TargetBuffer
    {
    public:
        TargetBuffer(int width, int height);
        ~TargetBuffer();

        void Activate();
        void AttachBuffer(int id, boost::shared_ptr<TextureBuffer> buffer);

    private:
        GLuint fbo;
        boost::shared_ptr<TextureBuffer> attached;
    };

    class PingPongBuffer: public DataBuffer
    {
    public:
        PingPongBuffer(boost::shared_ptr<TargetBuffer> target, int id,
                       boost::shared_ptr<TextureBuffer> front, boost::shared_ptr<TextureBuffer> back);
        ~PingPongBuffer();

        virtual size_t GetSize() const;
        virtual void Write(const void *src, size_t offset, size_t amount);
        virtual void Read(void *dst, size_t offset, size_t amount);
        virtual void Activate(int unit);

        void Pong();

    private:
        boost::shared_ptr<TargetBuffer> target;
        int targetId;
        boost::shared_ptr<TextureBuffer> front;
        boost::shared_ptr<TextureBuffer> back;
        int activeUnit;
    };

    class Program
    {
        Program();
    public:
        ~Program();

        void Activate();

        int GetUniformLocation(const std::string &name) const;
        void SetUniform(int loc, int v1);
        
        template <typename T1>
        void SetUniform(const std::string &name, T1 v1)
        {
            SetUniform(GetUniformLocation(name), v1);
        }

        static boost::shared_ptr<Program> CreateFromCode(const std::string &vertex, const std::string &fragment, const std::string &name=std::string(""));
        static boost::shared_ptr<Program> CreateFromFile(const std::string &vertex, const std::string &fragment);

    private:
        GLuint vertex, fragment, program;
    };
}

#endif //XTOOL_GPU_HPP

